package com.crr.service.impl;

import cn.hutool.core.map.MapUtil;
import com.crr.bean.Product;
import com.crr.bean.ProductBanner;
import com.crr.common.bean.PageBean;
import com.crr.common.crud.BaseServiceImpl;
import com.crr.mapper.ProductBannerMapper;
import com.crr.mapper.ProductMapper;
import com.crr.service.ProductService;
import com.crr.common.utils.Assert;
import com.crr.utils.RedisUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl extends BaseServiceImpl<Product, ProductMapper> implements ProductService {
    @Autowired
    ProductBannerMapper productBannerMapper;
    @Autowired
    HttpServletRequest request;
    @Autowired
    RedisUtils redisUtils;


    @Transactional
    @Override
    public int save(Product entity) {
        int res = super.save(entity);
        Assert.isNotSuccess(res, "产品保存失败");
        if (null != entity.getBanner() && entity.getBanner().size() > 0) {
            res = productBannerMapper.save(entity.getId(), entity.getBanner());
            Assert.isNotSuccess(res, "轮播图保存失败");
        }
        return 1;
    }

    @Transactional
    @Override
    public int update(Product entity) {
        int res = super.update(entity);
        Assert.isNotSuccess(res, "修改产品失败");
        //删除之前的轮播图 保存新增的轮播图
        productBannerMapper.deleteBannerById(entity.getId());
        //查询产品的轮播图
        List<ProductBanner> query = productBannerMapper.query(entity.getId());
        //获取图片保存的相对路径
        for (ProductBanner productBanner : query) {
            rmPicUrl(productBanner.getPicurl());
        }
        if (null != entity.getBanner() && entity.getBanner().size() > 0) {
            res = productBannerMapper.save(entity.getId(), entity.getBanner());
            Assert.isNotSuccess(res, "轮播图保存失败");
        }
        return 1;
    }


    /**
     * 服务器上删除图片
     *
     * @param path 图片保存的相对路径
     */
    void rmPicUrl(String path) {
        //获取服务器根路径
        String root = request.getServletContext().getRealPath("/");
        System.out.println(root + path);
        File file = new File(root + path);
        file.delete();
    }


    @Override
    public PageBean queryCust(Map map) {
        int page = MapUtil.getInt(map, "page");
        int pageSize = MapUtil.getInt(map, "pageSize");
        //设置分页
        PageHelper.startPage(page, pageSize);// 后面紧跟查询
        List<Map> list = baseMapper.queryCust(map);

        //获取分页信息 将查询结果封装一下
        PageInfo info = new PageInfo(list);

        return new PageBean(page, pageSize, info.getTotal(), list);
    }

    @Override
    public List<Map> queryAll() {
        List<Map> list;
        String key ="goodslist";
        Object o = redisUtils.get(key);
        if(null==o){
            //缓存中没有则查数据库
             list = baseMapper.queryAll();
            //将查询数据存入缓存
            redisUtils.setEx(key,list,60);
        }else {
            list=(List<Map>) o;
        }
        return list;
    }

}
